//
//  JLMainViewController.swift
//  新浪微博
//
//  Created by linlin on 15/11/22.
//  Copyright © 2015年 jilin. All rights reserved.
//

import UIKit

class JLMainViewController: UITabBarController {
    
    //加private就会崩
     func composeClick () {
        print("点击了")
    }
    
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

       
//        tabBar.tintColor = UIColor.orangeColor()
        
        
        let HoneCV = JLHomeController()
        addChildViewController(HoneCV, title: "首页", imageName: "tabbar_home")
        
        
        let MessageCV = JLMessageController()
        addChildViewController(MessageCV, title: "信息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController(), title: "", imageName: "")
        
        let ProfileCV = JLProfileController()
        addChildViewController(ProfileCV, title: "我", imageName: "tabbar_profile")
        
        
        let DiscoverCV = JLDiscoverController()
        addChildViewController(DiscoverCV, title: "发现", imageName: "tabbar_discover")
        
        
        
    }

    //为tabbarItem添加控制器
    private func addChildViewController(controller: UIViewController, title: String, imageName: String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //tabbar_home_highlighted选择状态下的图片
        let selectedImageName = imageName + "_highlighted"
        //不要系统渲染图片
        //Automatic
        //case AlwaysOriginal
        //case AlwaysTemplate
        
        let highlightImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        controller.tabBarItem.selectedImage = highlightImage
        
        //设置字体颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        let nav = UINavigationController(rootViewController: controller)
        
        addChildViewController(nav)
        
    }
    
    
    //添加发微博按钮 : 按钮要比TabBar上的按钮后添加 才会在上面 能点击到
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //发微博按钮的fram
        let width = tabBar.frame.width / 5
        composeButton.frame = CGRectMake(2*width - 5, 0, width + 10, tabBar.frame.height)
        
        
    }



    
   
    //懒加载发微博按钮
    lazy var composeButton: UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)

        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
    }()
    
    
    
    
    
    
    
    
    


}
